package study.ct.com.retrofitmodule;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import study.ct.com.retrofitmodule.bin.BookSearchResponse;
import study.ct.com.retrofitmodule.bin.TvLoginOk;
import study.ct.com.retrofitmodule.service.BlueService;
import study.ct.com.retrofitmodule.service.TvService;

/*
* test retrofit   ::http://duanyytop.github.io/2016/08/06/Retrofit%E7%94%A8%E6%B3%95%E8%AF%A6%E8%A7%A3/
* */
public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "run: MainThread==" + Thread.currentThread().getName());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.douban.com/v2/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        BlueService service = retrofit.create(BlueService.class);

        getHttpDataGet(service);
        getHttpDataPost();

    }


    private void getHttpDataGet(BlueService service) {

        final Call<BookSearchResponse> call = service.getSearchBooks("小王子", "", 0, 3);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "run: threadName==" + Thread.currentThread().getName());
                try {
                    //同步请求
                    /*BookSearchResponse response=call.execute().body();
                    Log.i(TAG, "onStart: response=="+response.getBooks().get(0).getOrigin_title());*/
                    call.enqueue(new Callback<BookSearchResponse>() {
                        @Override
                        public void onResponse(Call<BookSearchResponse> call, Response<BookSearchResponse> response) {
                            Log.i(TAG, "onResponse: ==========" + response.body().getBooks().get(0).getOrigin_title());
                        }

                        @Override
                        public void onFailure(Call<BookSearchResponse> call, Throwable t) {
                            Log.i(TAG, "onFailure: ==============");

                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    private void getHttpDataPost(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://wap.crunii.com:81/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        final TvService service = retrofit.create(TvService.class);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "run: threadName==" + Thread.currentThread().getName());
                Map<String, String> map = new HashMap<String, String>();
                map.put("mobile", "18983001336");
                map.put("code", "001336");
                Gson gson=new Gson();

                String route = gson.toJson(map);
                RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), route);
                Call<TvLoginOk> call=service.login(body);

                call.enqueue(new Callback<TvLoginOk>() {
                    @Override
                    public void onResponse(Call<TvLoginOk> call, Response<TvLoginOk> response) {
                        Log.i(TAG, "onResponse: threadName==" + Thread.currentThread().getName());
                        Log.i(TAG, "onResponse: status="+response.code());
                        Log.i(TAG, "onResponse: ==="+response.body());
                    }

                    @Override
                    public void onFailure(Call<TvLoginOk> call, Throwable t) {
                        Log.i(TAG, "onFailure: =========");
                    }
                });

            }
        }).start();

    }
}


